var off=0.01;

function addNumber(id) {
	for(var i=0;i<carts.length;i++)
	{
		if(carts[i]["oid"]==id)
		var left=document.getElementById("left_"+carts[i]["pid"]);
	}
	if(left.value==0)
	{
	alert("不能再增加了，库存不足");
	return;
	}
	left.value=parseInt(left.value)-1;
	//alert(left.value);
	var tar = document.getElementById("product_book_"+id);
	tar.innerHTML++;
	var discount=document.getElementById("product_discount_"+id);
	var user_level=document.getElementById("user_level").value;
	var should=document.getElementById("product_should_"+id);
	var price=document.getElementById("product_price_"+id);
	discount.innerHTML=parseFloat(((parseFloat(tar.innerHTML)*parseFloat(price.innerHTML)*parseFloat(off)*parseFloat(user_level)).toFixed(1))).toFixed(2);
	should.innerHTML=parseFloat(((parseFloat(tar.innerHTML)*parseFloat(price.innerHTML)*parseFloat((1-off*user_level))).toFixed(1))).toFixed(2);
	total();
	change();
}

function minuteNumber(id) {
	var tar = document.getElementById("product_book_"+id);
	if (tar.innerHTML == 1) {
		//tar.innerHTML = 1;
		return;
	}
	tar.innerHTML--;
	for(var i=0;i<carts.length;i++)
	{
		if(carts[i]["oid"]==id)
		var left=document.getElementById("left_"+carts[i]["pid"]);
	}
	left.value=parseInt(left.value)+1;
	//alert(left.value);
	var discount=document.getElementById("product_discount_"+id);
	var user_level=document.getElementById("user_level").value;
	user_level=parseFloat(user_level);
	var should=document.getElementById("product_should_"+id);
	var price=document.getElementById("product_price_"+id);
	discount.innerHTML=parseFloat(((parseFloat(tar.innerHTML)*parseFloat(price.innerHTML)*parseFloat(off)*parseFloat(user_level)).toFixed(1))).toFixed(2);
	should.innerHTML=parseFloat(((parseFloat(tar.innerHTML)*parseFloat(price.innerHTML)*parseFloat((1-off*user_level))).toFixed(1))).toFixed(2);
   total();
   change();
}

function change(){
	document.getElementById("save").style.display = "block";
	document.getElementById("ok").style.display = "none";
}

function total(){
	//alert(carts.length);
	var total=10;
	for(var i=0;i<carts.length;i++)
		{
		var num=document.getElementById("product_should_"+carts[i]["oid"]);
		total=total+parseFloat(num.innerHTML);
		}
   var tol=document.getElementById("total");
   tol.innerHTML=total.toFixed(2);
}

function selectall(ev){
	//var all=document.getElementById("checkbox_all");
	//alert(ev.checked);
	ev.value=ev.checked;
	for(var i=0;i<carts.length;i++)
	{
	var one=document.getElementById("checkbox_"+carts[i]["oid"]);
    one.checked=ev.checked;
    one.value=ev.checked;
	}
}

function savecart()
{
	for(var i=0;i<carts.length;i++)
	{
   var one=document.getElementById("count_id_"+carts[i]["oid"]);
   var one_v=document.getElementById("product_book_"+carts[i]["oid"]);
   one.value=one_v.innerHTML;
   //alert(one.value);
   var two=document.getElementById("should_id_"+carts[i]["oid"]);
   var two_v=document.getElementById("product_should_"+carts[i]["oid"]);
   two.value=two_v.innerHTML;  
	}
	 var flag=document.getElementById("id_flag");
	flag.value="save";

}
function selectone(ev)
{
	ev.value=ev.checked;
	//alert(ev.value);
	}
function deletecart(ev)
{
	if(!confirm("您确定要批量删除吗?")) 
	{
		event.returnValue=false;
		return;
	}
	
	var flag=document.getElementById("id_flag");
	flag.value="delete";
	}
function paycart()
{
	var total=document.getElementById("total").innerHTML;
	window.location.href="book.php?total="+total;
	}
function deleteone(id)
{
	if(!confirm("您确定要删除此项吗?")) 
	{//如果是true
		event.returnValue=false;
		return;
	}
	//alert("deleting"+id);
	var checkall=document.getElementById("checkbox_all");
	checkall.checked=false;
	checkall.value=false;
	for(var i=0;i<carts.length;i++)
	{
	var one=document.getElementById("checkbox_"+carts[i]["oid"]);
    one.checked=false;
    one.value=false;
    //alert(one.value);
	}
	var oneProduct=document.getElementById("checkbox_"+id);
	oneProduct.checked=true;
	oneProduct.value=true;
	var flag=document.getElementById("id_flag");
	flag.value="delete";
	document.cartform.submit();
	}
